<template>
    <div class="searchCom" >
        <div>
            <!--热搜榜-->
            <div class="hot_search" v-if="!suggest">
                <div class="title">热搜榜</div>
                <div class="search_item" 
                v-for="(item,index) in hotSearch"
                :key="index"
                @click="go(item.searchWord)"
                >
                    <div class="num" v-text="index+1" :class="index < 3 ? 'superHot' : ''"></div>
                    <div class="content">
                        <div class="name">{{item.searchWord}}<span class="hotNum">{{item.score}}</span> <span class="status" v-if="item.iconType == 1">HOT</span></div>
                        <div class="description">{{item.content}}</div>
                    </div>
                </div>
            </div>
            <!--搜索建议-->
            <div class="searchSuggest" v-else>
                <div class="goResult" @click="go(keyWords)">
                    搜"<span class="keywords">{{keyWords}}</span>"的相关结果<span class="iconfont iconarrow_right"></span>
                </div>

                <div class="title" v-if="order.songs">
                    <span class="nav_item iconfont  iconyinle1"></span>
                    单曲
                </div>
                <div class="songs" v-if="order.songs">
                    <div class="result_item"
                    v-for="(item,index) in searchSuggestList.songs"
                    :key="index"
                    >{{item.name}} - {{item.artists[0].name}}</div>
                </div>
                <div class="title" v-if="order.artists">
                    <span class="nav_item iconfont  iconuser"></span>
                    歌手
                </div>
                <div class="songs" v-if="order.artists">
                    <div class="result_item"
                    v-for="(item,index) in searchSuggestList.artists"
                    :key="index"
                    >{{item.name}}</div>
                </div>
                <div class="title" v-if="order.albums">
                    <span class="nav_item iconfont  iconzhuanjiguangpan"></span>
                    专辑
                </div>
                <div class="songs" v-if="order.albums">
                    <div class="result_item"
                    v-for="(item,index) in searchSuggestList.albums"
                    :key="index"
                    >{{item.name}} - {{item.artist.name}}</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import {_getHotSearch} from '../../../network/search'
import bus from '../../../util/bus'
export default {
    name: 'searchCom',
    props: [
    ],
    data() {
        return {
            // 热搜列表
            hotSearch:[],
            suggest:false,
            keyWords:'',
            // 是否输入显示搜索建议
            searchSuggestList:null,
            // 歌曲、歌手、专辑三项是否显示
            order:{
                songs:false,
                artists:false,
                albums:false,
            }

        };
    },
    computed: {

    },
    created() {
        // 获取热搜
        _getHotSearch()
        .then(res => {
            this.hotSearch = res.data.data
        })
        bus.$on('suggest',(res => {
            this.suggest = res
        }))
        // 获取搜索建议
        bus.$on('searchSuggest',(res => {
            this.order ={
                songs:false,
                artists:false,
                albums:false,
            }
            console.log(res)
            res.data.order.forEach(val => {
                this.order[val] = true
            })
            console.log(this.order)
            this.searchSuggestList = res.data
            this.keyWords = res.key
            this.suggest = true
        }))
    },
    mounted() {

    },
    watch: {

    },
    methods: {
        // 跳转搜索结果页面
        go(keywords){
            this.$router.push({path:'/search',query:{keywords}})
            // bus.$emit('searchFlag',true)
        }
    },
    components: {

    },
};
</script>

<style scoped lang="less">
@backolor:#fff;
@headTextColor:#eec1c1;
@secondColor:#ccc;
@lightolor:#E03F40;
.position_flex(@jc:space-between,@ai:center){
  display: flex;
  align-items: @ai;
  justify-content: @jc;
  
}
.itemStyle{
    padding: 0 10px;
    margin: 10px 0
}
.searchCom{
    position: absolute;
    top:49px;
    left: 331px;
    width: 500px;
    max-height: 570px;
    color: #000;
    overflow: auto;
    background-color: @backolor;
    transition: all .5s ease;
    z-index: 99;
    .hot_search{
        transition: all .5s ease;
        div{
            .itemStyle;
        }
        .search_item{
            .position_flex(flex-start);
            width: 100%;
            box-sizing: border-box;
            cursor: pointer;
            
            .superHot{
                color: @lightolor;
            }
            .content{
                font-size: 14px;
                .name{
                    font-size: 16px;
                    font-weight: bold;
                    span{
                        margin-left: 10px;
                        font-size: 14px;
                        
                    }
                    .hotNum{
                        color: @secondColor;
                        font-weight: normal;
                    }
                    .status{
                        color: @lightolor;
                        font-style: italic;
                    }

                }
                .description{
                    color: @secondColor;
                }
            }
            &:hover{
                background-color: #f5f6fa;
            }
        }
    }
    .searchSuggest{
        transition: all .5s ease;
        font-size: 14px;
        div{
            padding: 10px;
            cursor: pointer;
        }
        .goResult{
            .keywords{
            color: blue;
            }
        }
        .title{
            background-color: #F5F5F7;    
        }
        .songs{
            padding: 0;
        }
        .result_item{
            padding: 10px 30px;
            background-color: #fff;
            &:hover{
                background-color: #f5f6fa;
            }
        }
    }
}
</style>
